<?php
namespace app\web\controller;

use app\model\Theme; 

class Clotterypage extends Base
{
    public function Clotterypage()
    {
global $_G, $_P;
$themeid = $_P['themeid'];
$op = $_P['op'];
$id = $_P['id'];

if (empty($op)) {
    $lotterys = db_getall(t_clotterylist, array('tid' => $themeid, 'enable' => 0));
    foreach ($lotterys as  &$v) {
        $v['icon'] = tomedia($v['icon']);
        unset($v);
    }
    $cacheIcon = db_getall(t_2019fans, null, array('avatar'));
    $config = Theme::getConfig($themeid);
    include $this->template("web/clotterypage");
} else if ($op == 'listuser') {

    $user = db_getall(t_record,array('status'=>1));

    $user = array_combine(array_column($user, 'openid'), $user);

    $kickUser = safe_gpc_int($_P['kick']);
    $cheatUser = $_P['cheat'];
    $virtual = safe_gpc_int($_P['virtual']);
    if (!empty($cheatUser))
        $cheatUser = explode(',',$cheatUser);
    $condiction = array('tid' => $themeid);
    $kickUsers = array();
    if ($kickUser != 1) {
        $condiction['id'] = $_P['gid'];
        $kickUsers = db_getall(t_clotteryrecode, $condiction, array("users"));
    }
    
    $kick = array();
    foreach ($kickUsers as  $i) {
        $kick = array_merge($kick, explode(",",$i["users"]));
    }
    $kick = array_unique($kick);

    $cheatUsers = array();
    if (!empty($cheatUser)) {
        foreach ($user as  $u) {
            if (in_array($u['mobile'], $cheatUser)) {
                array_push($cheatUsers,$u);
            }
        }
    }
    if (!empty($kick)) {
        foreach ($kick as $v) {
            $u = $user[$v];
            if (!empty($u)) {
                if (!in_array($u, $cheatUsers))
                    unset($user[$v]);
            }
        }
    }
    if($virtual > 0){
        $status = rand(1,800);
        $xunisql = "SELECT * FROM " . tablename('2019xuni_fans') . " where
                     id >". $status ." limit ".$virtual;
        $virtualUser = db_select ( $xunisql );
        foreach ($virtualUser as  &$u) {
            $u['openid'] = 'vuser' . $u['id'];
            unset($u);
        }
        $cheatUsers = array_merge($cheatUsers,$virtualUser);
    }
    $fillUsers = db_getslice(t_2019fans,null,array(2,100));
    foreach ($fillUsers as  &$fv) {
        $fv['openid'] = 'vuser'.$fv['id'];
    }
    echo (json_encode(array('user' => array_values($user), 'cheat' => $cheatUsers,'fill'=>$fillUsers)));
} else if ($op == "sr") {
    $u = $_P['user'];
    $r = $insertid = db_insert(t_clotteryrecode, array('tid' => $themeid, 'id' => $_P['gid'], 'users' => implode(",",$u),'time'=>time()));
    echo ($r);
}else if($op == "fake"){
    $fake = safe_gpc_int($_P['fake']);
    $config = Theme::getConfig($themeid);
    $config['fakejoin'] = $fake;
    $config['faketuan'] = $fake * 10;
    $config['fakescan'] = $fake * 30;
    echo json_encode(array("message"=>Theme::setConfig($config,$themeid,$_G['mpid'])));
}else if($op == 'lr'){
    $r = db_getall(t_clotteryrecode, array('tid' => $themeid));
    $result = array();
    foreach ($r as $v) {
        $gift = db_get(t_clotterylist, array('tid' => $themeid,'id'=>$v['id']));
        $gift['icon'] = tomedia($gift['icon']);
        $open = explode(",",$v["users"]);
        $params = str_replace("[","",json_encode(array_values($open)));
        $params = str_replace("]","",$params);
        $params =  str_replace('"',"'",$params) ;
        $sql = "select nickname,avatar,openid from ". tablename(t_record) . " where status=1 and openid in (" . $params . ")"; 
        $users = db_select($sql);
        $virualUser = array_filter($open,function($i){
            return strpos($i, 'vuser') === 0;
        });
        if(!empty($virualUser)){
            foreach ($virualUser as  &$v) {
                $v = str_replace('vuser', '', $v);
                unset($v);
            }
            $params = str_replace("[","",json_encode(array_values($virualUser)));
            $params = str_replace("]","",$params);
            $params =  str_replace('"',"'",$params) ;
            $sql = "select id,nickname,avatar from ". tablename(t_2019fans) . " where id in (" . $params . ")";
            $vuser = db_select($sql);
            foreach ($vuser as  &$u) {
                $u['openid'] = 'vuser' . $u['id'];
                unset($u);
            }
            $users = array_merge($users,$vuser);
        }
        array_push($result,array('gift'=>$gift,'recode'=>$users));
    }
    
    echo json_encode($result);
    
}
}
}